package cc.wanforme.st.server.rw;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;

import cc.wanforme.st.server.util.SimpleJackson;

public class RemapTest {

	public static void main(String[] args) throws IOException {
		File rightFile = new File("C:\\Users\\Administrator\\Downloads\\otfcc-win64-0.10.4\\siyuan.json");
		File wrongFile = new File("C:\\Users\\Administrator\\Downloads\\otfcc-win64-0.10.4\\siyuan-mix.json");

		byte[] rightBs = Files.readAllBytes(rightFile.toPath());
		Map<String, String> rightMap = getMapFromFontJson(new String(rightBs));

		byte[] wrongBs = Files.readAllBytes(wrongFile.toPath());
		Map<String, String> wrongMap = getMapFromFontJson(new String(wrongBs));
		
	    // 普义上中文范围 0x4E00 至 0x9FFF
	    String raw = "百度百科是百度公司推出的一部内容开放、自由的网络百科全书。其测试版于2006年4月20日上线，正式版在2008年4月21日发布，截至2023年4月，百度百科已经收录了超2700万个词条，参与词条编辑的网友超过770万人，几乎涵盖了所有已知的知识领域。 [1]\r\n"
	        + "“世界很复杂，百度更懂你”，百度百科旨在创造一个涵盖各领域知识的中文信息收集平台。百度百科强调用户的参与和奉献精神，充分调动互联网用户的力量，汇聚上亿用户的头脑智慧，积极进行交流和分享。同时，百度百科实现与百度搜索、百度知道的结合，从不同的层次上满足用户对信息的需求。";

	    List<CodeMap> rightList = rightMap.keySet().stream()
				.map(wrongUnicode -> new CodeMap(wrongUnicode, rightMap.get(wrongUnicode)))
				.collect(Collectors.toList());
	    List<CodeMap> wrongList = wrongMap.keySet().stream()
				.map(wrongUnicode -> new CodeMap(wrongUnicode, wrongMap.get(wrongUnicode)))
				.collect(Collectors.toList());
	    // 转换成错误的字符串
	    //convertToWrong(raw, rightMap, wrongMap);
	    convertToWrong(raw, rightList, wrongList);
		System.out.println("end");
	}

	public static class CodeMap {
		private int unicode;
		private String glyphId; // 一般是数字

		public CodeMap(int unicode, String glyphId) {
			this.unicode = unicode;
			this.glyphId = glyphId;
		}
		public CodeMap(String unicode, String glyphId) {
			this.unicode = Integer.parseInt(unicode);
			this.glyphId = glyphId;
		}

		// ... getter setter
		public int getUnicode() {
			return unicode;
		}

		public void setUnicode(int unicode) {
			this.unicode = unicode;
		}

		public String getGlyphId() {
			return glyphId;
		}

		public void setGlyphId(String glyphId) {
			this.glyphId = glyphId;
		}
	}

	/** 读取映射关系 （键为unicode，值为字形索引） */
	private static Map<String, String> getMapFromFontJson(String jsonStr) throws JsonProcessingException {
		TypeReference<HashMap<String, Object>> typeReference = new TypeReference<HashMap<String, Object>>() {
		};
		HashMap<String, Object> font = SimpleJackson.toAny(jsonStr, typeReference);

		@SuppressWarnings("unchecked")
		Map<String, String> cmap = (Map<String, String>) font.get("cmap");
		return cmap;
	}
	
	private static void convertToWrong(String raw, List<CodeMap> rightMap, List<CodeMap> wrongMap) {
		StringBuilder sb = new StringBuilder();
		for (char c : raw.toCharArray()) {
			Optional<CodeMap> opt = rightMap.stream().filter(e -> e.getUnicode() == c).findAny();
			if (!opt.isPresent()) {
				System.err.println("There's no glyphId for '" + c + "'");
				continue;
			}

			String id = opt.get().getGlyphId();
			Optional<CodeMap> wrongOpt = wrongMap.stream().filter(e -> e.getGlyphId().equals(id)).findAny();
			if (!wrongOpt.isPresent()) {
				System.err.println("There's no wrong-glyphId for '" + c + "'");
				continue;
			}

			char wrongUnicode = (char) wrongOpt.get().getUnicode();
			sb.append(wrongUnicode);
		}

		System.out.println("final: \n" + sb.toString());
	}

	/** 将字符串根据错误的映射关系转换成错误的字符串 
	private static void convertToWrong(String raw, Map<String, String> rightMap, Map<String, String> wrongMap) {
		StringBuilder sb = new StringBuilder();
		
		// 用于方便 从字形id查询字符 （逆向查询）
		List<CodeMap> wrongList = wrongMap.keySet().stream()
			.map(wrongUnicode -> new CodeMap(wrongUnicode, wrongMap.get(wrongUnicode)))
			.collect(Collectors.toList());
		
		for (char c : raw.toCharArray()) {
			String key = c + "";
			if (!rightMap.containsKey(key)) {
				sb.append(c);
				System.err.println("There's no glyphId for '" + c + "'");
				continue;
			}

			// 根据id查询错误的unicode
			String id = rightMap.get(key);
			Optional<CodeMap> wrongOpt = wrongList.stream().filter(e -> e.getGlyphId().equals(id)).findAny();
			if (!wrongOpt.isPresent()) {
				sb.append(c);
				System.err.println("There's no wrong-glyphId for '" + c + "'");
				continue;
			}

			// 追加错误的字符
			char wrongUniStr = (char) wrongOpt.get().getUnicode();
			sb.append(wrongUniStr);
		}

		System.out.println("final: \n" + sb.toString());
	}
	 */
}
